package cn.ztuo.bitrade.config;

import cn.ztuo.bitrade.entity.ContractCoin;
import cn.ztuo.bitrade.runnable.RobotOrderRunnable;
import cn.ztuo.bitrade.service.ContractCoinService;
import cn.ztuo.bitrade.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AfterServiceStarted implements ApplicationRunner {
    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractCoinService contractCoinService;
    /**
     * 会在服务启动完成后立即执行
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("Successful service startup!");
        List<ContractCoin> coins = contractCoinService.findAllEnabled();
        Map<String,Integer> map = RobotOrderRunnable.robotStatusMap;
        coins.stream().forEach(coin -> {
            if(coin.getRobotStatus()!=null&&coin.getRobotStatus()==1){
                contractService.robotAddOrder(coin.getSymbol());
            }else{
                coin.setRobotStatus(0);
            }
            map.put(coin.getSymbol(),coin.getRobotStatus());
        });
        RobotOrderRunnable.robotStatusMap=map;
    }
}